package com.wu.shop.module.shop.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wkq.lib_base.adapter.KtAdapter
import com.wu.common.base.adapter.DataBindingViewHolder
import com.wu.shop.R
import com.wu.shop.databinding.ItemShopCarBinding
import com.wu.shop.module.shop.model.ShopCarInfo
import com.wu.shop.observable.ShopCarBuyObservable
import com.wu.shop.observable.shopcar.ShopCarBuyInfo
import com.wu.shop.util.ShopCarUtil

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2021/7/6
 *
 * 用途:
 */


class ShopCarAdapter(mContext: Context) : KtAdapter<ShopCarInfo>(mContext) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var viewDatabinding = DataBindingUtil.inflate<ItemShopCarBinding>(
            LayoutInflater.from(mContext),
            R.layout.item_shop_car,
            parent,
            false
        )

        var bindingHolder = DataBindingViewHolder(viewDatabinding.root)
        bindingHolder.binding = viewDatabinding
        return bindingHolder
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var bindingHolder = holder as DataBindingViewHolder
        var binding = bindingHolder.binding as ItemShopCarBinding
        binding.tvTitle.text = getItem(position)!!.shopName

        binding.rvContent.layoutManager = LinearLayoutManager(mContext)
        var mAdapter = ShopCarGoodsInfoAdapter(mContext);
        binding.rvContent.adapter = mAdapter
        mAdapter.addItems(getItem(position)!!.goodsList)

        if (getItem(position)!!.isSelect) {
            binding.checkbox.setBackgroundResource(R.drawable.iv_shop_select)
        } else {
            binding.checkbox.setBackgroundResource(R.drawable.iv_shop_defult)
        }

        binding.llCheck.setOnClickListener {
            getItem(position)!!.isSelect = !getItem(position)!!.isSelect
            if (getItem(position)!!.isSelect) {
                binding.checkbox.setBackgroundResource(R.drawable.iv_shop_select)
            } else {
                binding.checkbox.setBackgroundResource(R.drawable.iv_shop_defult)
            }
            mAdapter.getItems()!!.forEachIndexed { index, goodsInfo ->
                if (getItem(position)!!.isSelect) {
                    if (!getItem(position)!!.goodsList.get(index).isChecked ){
                        ShopCarUtil.processAddOrSub(
                            getItem(position)!!.goodsList.get(index).goodsId,
                            getItem(position)!!.goodsList.get(index).goodsPrice,
                            true ,
                            getItem(position)!!.goodsList.get(index).goodsNum
                        )
                    }
                    getItem(position)!!.goodsList.get(index).isChecked = true
                    goodsInfo.isChecked = true
                } else {
                    if (getItem(position)!!.goodsList.get(index).isChecked ){
                        ShopCarUtil.processAddOrSub(
                            getItem(position)!!.goodsList.get(index).goodsId,
                            getItem(position)!!.goodsList.get(index).goodsPrice,
                            false ,
                            getItem(position)!!.goodsList.get(index).goodsNum
                        )
                    }
                    getItem(position)!!.goodsList.get(index).isChecked = false
                    goodsInfo.isChecked = false
                }
            }
            mAdapter.notifyDataSetChanged()
        }

    }


}
